/**
 * Created by PyCharm.
 * User: Gary
 * Date: 11-8-16
 * Time: 下午7:53
 * To change this template use File | Settings | File Templates.
 */
var TargetTop = 515;
var GoldTargetLeft = 225;
var SilverTargetLeft = 695;
$(document).ready(function() {
    //浮云
    cloudsScrollSpeed = 70;
    cloudsStep = 1;
    cloudsCurrent = 0;
    cloudsImageWidth = 2247;
    cloudsHeaderWidth = 800;
    cloudsRestartPosition = -(cloudsImageWidth - cloudsHeaderWidth);
    cloudsInit = setInterval("cloudsScrollBg()", cloudsScrollSpeed);
    //浮云结束
    $(".Container .Ball").bind("click", function() {
        SelectBall(this);
    });
    $(".loginlink").bind("click",function(){
        gologin();
        return false;
    });
    $("#SubmitForm #Submit").bind("click",function(){
        if(LogedIn){
        $("#SubmitForm #Ticket").val(FormTicket());
        $("#SubmitForm").ajaxSubmit({
            dataType:"json",
            success:function(data){
                if(data.Success){
                    var notice = new Notification(data.Message);
                    notice.autoremove(5);
                    var selects = $(".Container .Selected");
                    var length = selects.length;
                    for(var i=0;i<length;i++){
                        $(selects[i]).click();
                    }
                    setTimeout(function(){
                        $("#TicketCount").fadeOut(500);
                    },2000);
                }else{
                    var notice = new Notification("错误：" + data.Message);
                    notice.autoremove(5);
                }
            },
            error:function(){
                var notice = new Notification("连接错误");
                notice.autoremove(5);
            }
        });
        }else{
            gologin();
        }
        return false;

    });
    if(Ticket){
        var TicketArray = Ticket.split(':');
        var Ticket0 = TicketArray[0].split(',');
        var Ticket1 = TicketArray[1].split(',');
        var Ticket2 = TicketArray[2].split(',');
        var Ticket3 = TicketArray[3].split(',');
        var length = Ticket0.length;
        for(var i=0;i<length;i++){
            if(Ticket0[i]!=""){
                $("#GoldBallContainer #Gold-" + Ticket0[i]).click();
            }
        }
        length = Ticket2.length;
        for (var i = 0; i < length; i++) {
            if (Ticket2[i] != "") {
                $("#GoldBallContainer #Gold-" + Ticket2[i]).click();
            }
        }
        length = Ticket1.length;
        for(var i=0;i<length;i++){
            if(Ticket1[i]!=""){
                $("#SilverBallContainer #Silver-" + Ticket1[i]).click();
            }
        }
        length = Ticket3.length;
        for(var i=0;i<length;i++){
            if(Ticket3[i]!=""){
                $("#SilverBallContainer #Silver-" + Ticket3[i]).click();
            }
        }
        length = Ticket2.length;
        for (var i = 0; i < length; i++) {
            if (Ticket2[i] != "") {
                $("#GoldBallTarget #Gold-" + Ticket2[i]).click();
            }
        }
        length = Ticket3.length;
        for(var i=0;i<length;i++){
            if(Ticket3[i]!=""){
                $("#SilverBallTarget #Silver-" + Ticket3[i]).click();
            }
        }

    }
});
function cloudsScrollBg(){
    cloudsCurrent -= cloudsStep;
    if (cloudsCurrent == cloudsRestartPosition){
      cloudsCurrent = 0;
    }
    $('.clouds').css("background-position",cloudsCurrent+"px 0");
}
function gologin(){
    var url = "/Login/response_weibo/?Ticket=" + FormTicket();
    location.href = url;
}
function Notification(message) {
    this.notice = $("<div class='notice'>" + message + "</div>");
    this.notice.appendTo('#noticearea');
    this.remove = function () {
        $(this.notice).remove();
    }
    this.show = function () {
        $(this.notice).fadeIn(1500);
    }
    this.autoremove = function (secondes) {
        var notice = $(this.notice).fadeIn(100).delay(secondes * 1000).fadeOut(100,function(){
            notice.remove();
        });
    }
    return this;
}
function SelectBall(obj) {
    if ($(obj).hasClass("GoldBall")) {
        $(obj).unbind("click").clone().appendTo("#GoldBallTarget").css({left:'315px',top:'285px'}).show().addClass("Tuo");
        GoldBallOrganize();
        $(obj).addClass("Selected").bind('click', function() {
            CancelBall(this);
        });
    } else {
        $(obj).unbind("click").clone().appendTo("#SilverBallTarget").css({left:'765px',top:'285px'}).show().addClass("Tuo");
        SilverBallOrganize();
        $(obj).addClass("Selected").bind('click', function() {
            CancelBall(this);
        });
    }
}
function FormTicket() {
    var ticket = '';
    var goldTuo = $("#GoldBallTarget .Tuo");
    var silverTuo = $("#SilverBallTarget .Tuo");
    var goldTuoCount = goldTuo.length;
    var silverTuoCount = silverTuo.length;
    var goldDan = $("#GoldBallTarget .Dan");
    var silverDan = $("#SilverBallTarget .Dan");
    var goldDanCount = goldDan.length;
    var silverDanCount = silverDan.length;
    for (var i = 0; i < goldTuoCount; i++) {
        ticket = ticket + $(goldTuo[i]).html();
        if (i < goldTuoCount - 1) {
            ticket = ticket + ',';
        }
    }
    ticket = ticket + ':';
    for (var i = 0; i < silverTuoCount; i++) {
        ticket = ticket + $(silverTuo[i]).html();
        if (i < silverTuoCount - 1) {
            ticket = ticket + ',';
        }
    }
    ticket = ticket + ':';
    for (var i = 0; i < goldDanCount; i++) {
        ticket = ticket + $(goldDan[i]).html();
        if (i < goldDanCount - 1) {
            ticket = ticket + ',';
        }
    }
    ticket = ticket + ':';
    for (var i = 0; i < silverDanCount; i++) {
        ticket = ticket + $(silverDan[i]).html();
        if (i < silverDanCount - 1) {
            ticket = ticket + ',';
        }
    }
    return ticket;
}
function TicketCount() {
    var GoldCount=$("#GoldBallTarget .Ball").length;
    var SilverCount=$("#SilverBallTarget .Ball").length;
    if (GoldCount > 4 & SilverCount > 1) {
        var ticket = FormTicket();
        $.ajax({
            url:'http://127.0.0.1:8000/bigprize/count/?Ticket=' + ticket,
            success:function(data) {
                $("#TicketCount #Count").html(data);
                $("#TicketCount #Money").html(parseInt(data) * 2);
                $("#TicketCount").show();
            },
            error:function() {
                var notice = new Notification("算不出注数了！");
                notice.autoremove(2);
            }
        });
    } else {
        $("#TicketCount").hide();
    }
}
function CancelBall(obj) {
    if ($(obj).hasClass("GoldBall")) {
        var removedball = $("#GoldBallTarget").find("#" + $(obj).attr("id")).appendTo("#Moving").
            animate({left:'315px',top:'285px'}, 300, function() {
                removedball.remove();
                $(obj).removeClass("Selected").unbind("click").bind("click", function() {
                    SelectBall(obj);
                });
            });
        GoldBallOrganize();
    } else {
        var removedball = $("#SilverBallTarget").find("#" + $(obj).attr("id")).appendTo("#Moving").
            animate({left:'765px',top:'285px'}, 300, function() {
                removedball.remove();
                $(obj).removeClass("Selected").unbind("click").bind("click", function() {
                    SelectBall(obj);
                });
            });
        SilverBallOrganize();
    }
}
function GoldBallOrganize() {
    $("#GoldBallTarget .Ball").each(function(index, ball) {
        var top = TargetTop - Math.floor(index / 7) * 30;
        var left = GoldTargetLeft + (index % 7) * 30;
        $(ball).animate({top:top + "px"}, 300,function(){
            $(ball).animate({left:left + "px"}, 200);
        });
    });
    TicketCount();
    TestDanTuo();
}
function SilverBallOrganize() {
    $("#SilverBallTarget .Ball").each(function(index, ball) {
        var top = TargetTop - Math.floor(index / 7) * 30;
        var left = SilverTargetLeft + (index % 7) * 30;
        $(ball).animate({top:top + "px"}, 300,function(){
            $(ball).animate({left:left + "px"}, 200);
        });
    });
    TicketCount();
    TestDanTuo();
}
function ChangeDanTuo(obj) {
    if ($(obj).hasClass("Dan")) {
        $(obj).removeClass("Dan").addClass("Tuo");
    } else {
        $(obj).addClass("Dan").removeClass("Tuo");
    }
    TicketCount();
    TestDanTuo();
}
function Shake(objs) {
    $(objs).each(function(index, obj) {
        $(obj).animate({left:"-=2px"}, 20, function() {
            $(obj).animate({left:"+=5px"}, 20, function() {
                $(obj).animate({left:"-=5px"}, 20, function() {
                    $(obj).animate({left:"+=5px"}, 20, function() {
                        $(obj).animate({left:"-=5px"}, 20, function() {
                            $(obj).animate({left:"+=2px"}, 20);
                        });
                    });
                });
            });
        });
    });
}
function TestDanTuo() {
    $(".Target .Ball").unbind("click");
    $(".Target .Ball").css({cursor: "default"});
    if ($("#GoldBallTarget .Ball").length > 5) {
        if ($("#GoldBallTarget .Dan").length < 4) {
            $("#GoldDanNotice").slideDown(300);
            $("#GoldBallTarget .Ball").bind("click",
                function() {
                    ChangeDanTuo(this);
                }).css({cursor: "pointer"});
        } else {
            $("#GoldDanNotice").slideUp(300);
            $("#GoldBallTarget .Dan").bind("click",
                function() {
                    ChangeDanTuo(this);
                }).css({cursor: "pointer"});
            $("#GoldBallTarget .Tuo").bind("click",
                function(){
                    Shake($("#GoldBallTarget .Dan"));
                }
            ).css({cursor:"pointer"});
        }
    } else {
        $("#GoldDanNotice").slideUp(300);
        $("#GoldBallTarget .Dan").removeClass("Dan").addClass("Tuo");
    }
    if ($("#SilverBallTarget .Ball").length > 2) {
        $("#SilverDanNotice").slideDown(300);
        $("#SilverBallTarget .Ball").css({cursor: "pointer"});
        $("#SilverBallTarget .Dan").bind("click",function(){
            ChangeDanTuo(this);
        });
        $("#SilverBallTarget .Tuo").bind("click",function(){
            $("#SilverBallTarget .Dan").removeClass("Dan").addClass("Tuo");
            $(this).addClass("Dan").removeClass("Tuo");
            TicketCount();
            TestDanTuo();
        });
    } else {
        $("#SilverDanNotice").slideUp(300);
        $("#SilverBallTarget .Dan").removeClass("Dan").addClass("Tuo");
        $("#SilverBallTarget .Ball").css({cursor: "Default"});
    }
}